//
//  ChainNewsListVC.m
//  Chain
//
//  Created by zhaosh on 2018/4/2.
//  Copyright © 2018年 zhaosh. All rights reserved.
//

#import "ChainNewsListVC.h"
#import "ChainNoDataView.h"
#import "ChainNewsInfo.h"
#import "ChainNewsCell.h"
#import "MJRefreshBackStateFooter.h"
#import "ChainNormalRefreshHeader.h"
#import "ChainNewsListModel.h"
#import "ChainNewsDetailVC.h"
#import "ChainNormalRefreshFooter.h"

@interface ChainNewsListVC ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)UITableView *tableview;
@property(strong,nonatomic)NSMutableArray *dataArr;
@property(strong,nonatomic)UIView *showNoDataView;
@end

@implementation ChainNewsListVC
{
    NSInteger _pageNum;
}

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray new];
    }
    return _dataArr;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
 
    [[NSNotificationCenter defaultCenter]postNotificationName:@"hiddenRedMarkView" object:nil];
    if (self.noDataView.hidden == NO && self.newsTypeId) {
        [self.tableview.mj_header beginRefreshing];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =[UIColor whiteColor];
    [self setupTableview];
    
    [self getNewData];
}

-(void)setupErrorView{
    if(self.newsTypeId){
        self.noDataView = [[ChainNoDataView alloc]initWithFrame:self.tableview.bounds];
        
        self.noDataView.hidden = YES;
        [self.tableview addSubview:self.noDataView];
    }
}

-(void)fetchListDataWithPage:(NSUInteger)pageNum{
    NSString *requestPage =[NSString stringWithFormat:@"%ld",_pageNum];
    NSDictionary *params = @{@"pageNo":requestPage,@"pageSize":@"10",@"type":self.newsTypeId,@"title":@""};
    NSString *requestUrl=[NSString stringWithFormat:@"%@%@",BASE_URL,@"/latoutiao/news/getNewsList"];
    [[ChainNetAPIClient sharedJsonClient]postRequestJsonDataWithPath:requestUrl withParams:params withHeader:nil andBlock:^(id data, NSError *error) {
        [self hideHUDafterDelay:0];
        
        NSDictionary *info = [NSDictionary dictionaryWithDictionary:data];
        if (error) {
            NSString *message=[info objectForKey:@"message"];
            [MBProgressHUD  showMessage:message ToView:nil RemainTime:0.5];
        }else{
            NSString *success=[NSString stringWithFormat:@"%@",[info objectForKey:@"success"]];
            if ([success isEqualToString:@"1"]) {
                
                if (_pageNum==1) {
                    [self.dataArr removeAllObjects];
                }
                NSArray *tempArray=[data valueForKeyPath:@"value.rows"];
                for (NSDictionary *dic in tempArray) {
                    ChainNewsListModel *info = [ChainNewsListModel yy_modelWithDictionary:dic];
                    [self.dataArr addObject:info];
                }
            }
        }
        [self.tableview reloadData];
        
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        
        self.noDataView.hidden = self.dataArr.count != 0;
    }];
}

-(void)setupTableview{
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-kTopHeight-49-53) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = AllViewBackgroundColor;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableview];
    
    //ios11有向上抖动问题
    NSString *version = [UIDevice currentDevice].systemVersion;
    if (version.doubleValue >= 11.0) {
        self.tableview.estimatedRowHeight = 0;
        self.tableview.estimatedSectionFooterHeight = 0;
        self.tableview.estimatedSectionHeaderHeight = 0;
        self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
 
    __weak typeof(self) weakSelf = self;
    //添加顶部下啦刷新视图
    self.tableview.mj_header = [ChainNormalRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf getNewData];
    }];
    
    //添加底部上提加载视图
    self.tableview.mj_footer = [ChainNormalRefreshFooter footerWithRefreshingBlock:^{
        [weakSelf getNextData];
    }];
    [self setupErrorView];
}

-(void)getNewData{
    _pageNum = 1;
    [self showLoading];
    [self fetchListDataWithPage:_pageNum];
}

-(void)getNextData{
    _pageNum++;
    [self fetchListDataWithPage:_pageNum];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChainNewsCell *cell = [ChainNewsCell cellWithTableView:tableView];
    cell.item = self.dataArr[indexPath.row];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChainNewsListModel *info = self.dataArr[indexPath.row];
    
    ChainNewsCell *cell = [ChainNewsCell cellWithTableView:tableView];
    cell.item = info;
    [cell layoutSubviews];
    CGFloat rowH = cell.item.rowHeight;
    cell = nil;
    return rowH;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ChainNewsListModel *info = self.dataArr[indexPath.row];
    
    ChainNewsDetailVC *detailVC =[[ChainNewsDetailVC alloc] init];
    detailVC.detailId = info._id;
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
